# encoding=utf-8
import time
import datetime
import scrapy
from selenium import webdriver

from jipiao.items import JipiaoItem
from  jipiao.sendmsg import send_mail
class Myspider(scrapy.Spider):
    name = 'jipiao'
    start_urls = 'https://www.fliggy.com/?hlreferid=baidu.082076'
    startcity = u'哈尔滨'
    endcity = u'成都'
    jipiaoItem = JipiaoItem()
    dpdatestart = '2018-01-26'  # 出发日期
    now = datetime.datetime.now()
    crawltime = now.strftime('%Y-%m-%d %H:%M:%S')  # 抓取数据的年月日
    lastflightPrice = 2000
    postData = {"机票价格": lastflightPrice, "出发日期": dpdatestart,
                "起飞时间": 0,
                "到达时间": 0, "数据爬取时间": crawltime}
    print str(postData).decode('utf-8')
    def __init__(self):
        self.openbrowser(self.start_urls,self.startcity,self.endcity,self.dpdatestart,self.postData)
    #depdate=datetime.datetime.strptime('2018-01-28','%Y-%m-%d')
    #dcap = dict(DesiredCapabilities.PHANTOMJS)
    #dcap["phantomjs.page.settings.userAgent"] = (
    #"User-Agent:Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1 ")
    #browser = webdriver.PhantomJS(desired_capabilities=dcap
    def openbrowser(self,host,startcity,endcity,dpdatestart,postData):
        browser = webdriver.Firefox()
        browser.get(host)
        time.sleep(3)
        #print browser.page_source
        start= browser.find_element_by_xpath("//*[@id='J_FlightForm']/fieldset/div[2]/div/input[1]")
        start.clear()
        start.send_keys(startcity)
        end = browser.find_element_by_name("arrCityName")
        end.clear()
        end.send_keys(endcity)
        depDate = browser.find_element_by_name("depDate")
        depDate.clear()
        depDate.send_keys(dpdatestart)
        commit = browser.find_element_by_xpath("//*[@id='J_FlightForm']/fieldset/div[8]/span/button")#提交
        commit.click()
        time.sleep(5)
        loop_n=0;
        filghtline=u'航班信息'
        print filghtline
        while(loop_n <=5):

            print 'wait for open new page:'
            browser.switch_to.window(browser.window_handles[1])
            print browser.current_url,browser.title
            try:
               filghtline = browser.find_element_by_xpath("/html/body/div[5]/section[1]/div[5]/div[2]/table/tbody/tr/th[1]").text
            except:
                time.sleep(5)
                loop_n += 1
                if loop_n == 5:
                    print u'没有打开新的网页'
                    for handle in browser.window_handles:
                        browser.switch_to.window(handle)
                        print u'第i个窗口：', browser.title
                        break
                else:
                     continue
            else:
                print u'成功打开新的网页'
                loop_n=6
                FlightListPrice = browser.find_elements_by_class_name("J_FlightListPrice")  # 机票价格
                depListtime = browser.find_elements_by_class_name("flight-time-deptime")  # 出发时间
                arrListtime = browser.find_elements_by_class_name("s-time")  # 到达时间
                for index in range(len(FlightListPrice)):
                    flightPrice = FlightListPrice[index]
                    deptime = depListtime[index]
                    arrtime = arrListtime[index]
                    if int(flightPrice.text) <= int(postData['机票价格']):
                        postData = {"机票价格": flightPrice.text, "出发日期": dpdatestart,
                                    "起飞时间": deptime.text,
                                    "到达时间": arrtime.text, "数据爬取时间": self.crawltime}
                        # postData = str("机票价格", flightPrice.text,"\n出发日期", dpdate,"\n起飞时间", \
                        #            deptime.text,"\n到达时间", arrtime.text,"\n数爬取时间",crawltime)
                        # lastflightPrice=flightPrice.text

                    self.jipiaoItem["crawltime"] = self.crawltime
                    if flightPrice:
                        print '保存机票价格'
                        self.jipiaoItem["price"] = flightPrice.text,  # 保存机票价格
                    if deptime:
                        self.jipiaoItem["deptime"] = deptime.text
                    if arrtime:
                        self.jipiaoItem["arrtime"] = arrtime.text
                    if dpdatestart:
                        self.jipiaoItem["depdate"] = dpdatestart
                        # 爬取从26日到之后9天的机票数据
                textcontent = u'机票价格:' + postData['机票价格'] + u'\n出发日期:' + postData['出发日期'] + u'\n起飞时间:' + postData[
                    '起飞时间'] + u'\n到达时间:' + postData['到达时间'] + u'\n数据爬取时间:' + postData['数据爬取时间']
                print type(textcontent)
                for day in range(1, 9):
                    print u'爬取从26日到之后9天的机票数据'
                    dpdate = datetime.datetime.strptime(dpdatestart, '%Y-%m-%d') + datetime.timedelta(days=day)
                    dpdate = dpdate.strftime('%Y-%m-%d')
                    postData = self.jipiaoinformation(browser,dpdate, postData)
                    tmpcontent = u'机票价格:' + postData['机票价格'] + u'出发日期:' + postData['出发日期'] + u'起飞时间:' + postData[
                        '起飞时间'] + u'到达时间:' + postData['到达时间'] + u'数据爬取时间:' + postData['数据爬取时间']
                    textcontent = textcontent+'\n'+tmpcontent
                print textcontent
                sendemail = send_mail()
                KEY = 'iwsemthggjavbbbg'  # 授权码
                HostUserName = 'hit_zml@qq.com'
                ToUserName = '921783424@qq.com'
                Content = {'header': u'机票价格提醒', 'text': textcontent}
                sendemail.Send_mail(Content, HostUserName, KEY, ToUserName)

            print filghtline
        browser.quit()

    def jipiaoinformation(self,browser,dpdate,postData):
        date=browser.find_element_by_xpath('/html/body/div[5]/section[1]/div[1]/form/div[3]/label[1]/div/input')
        date.clear()
        date.send_keys(dpdate)
        browser.find_element_by_xpath('/html/body/div[5]/section[1]/div[1]/form/span/span/input').click()
        time.sleep(3)
        loop_n=0
        while(loop_n<5):
            try:
                FlightListPrice = browser.find_elements_by_class_name("J_FlightListPrice")  # 机票价格
                depListtime = browser.find_elements_by_class_name("flight-time-deptime")  # 出发时间
                arrListtime = browser.find_elements_by_class_name("s-time")  # 到达时间
                break
            except:
                time.sleep(3)
                loop_n+=1
        for index in range(len(FlightListPrice)):
            flightPrice = FlightListPrice[index]
            deptime = depListtime[index]
            arrtime = arrListtime[index]
            if int(flightPrice.text) <= int(postData['机票价格']):
                postData = {"机票价格": flightPrice.text, "出发日期": dpdate,
                            "起飞时间": deptime.text,
                            "到达时间": arrtime.text, "数据爬取时间": self.crawltime}
                # postData = str("机票价格", flightPrice.text,"\n出发日期", dpdate,"\n起飞时间", \
                #            deptime.text,"\n到达时间", arrtime.text,"\n数爬取时间",crawltime)
                # lastflightPrice=flightPrice.text

            self.jipiaoItem["crawltime"] = self.crawltime
            if flightPrice:
                print '保存机票价格'
                self.jipiaoItem["price"] = flightPrice.text,  # 保存机票价格
            if deptime:
                self.jipiaoItem["deptime"] = deptime.text
            if arrtime:
                self.jipiaoItem["arrtime"] = arrtime.text
            if dpdate:
                self.jipiaoItem["depdate"] = dpdate
            return postData
